package consul

import (
	"fmt"
	consulApi "github.com/hashicorp/consul/api"
	"google.golang.org/grpc"
	"google.golang.org/grpc/health"
	"google.golang.org/grpc/health/grpc_health_v1"
	"order-srv/libs"
)

var consulClient *consulApi.Client

// 初始化配置
func init() {
	// 1.创建连接consul客户端
	consulConfig := consulApi.DefaultConfig()
	consulConfig.Address = fmt.Sprintf(
		"%s:%d",
		libs.ConsulConfig.Address,
		libs.ConsulConfig.Port,
	) // consul 默认端口号
	_consulClient, err := consulApi.NewClient(consulConfig)
	if err != nil {
		panic(err)
		return
	}
	consulClient = _consulClient
}

// Health 实现grpc.Server内的健康检测方法
func Health(server *grpc.Server) {
	// grpc 支持健康检查,由consul执行健康检查所调用的TCP接口，直接这样写好了，具体内部已经实现好了
	// https://github.com/grpc/grpc/blob/master/doc/health-checking.md
	grpc_health_v1.RegisterHealthServer(server, health.NewServer())
}
